package cn.edu.sgu.www.mhxysy.controller.server;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.server.ServerDTO;
import cn.edu.sgu.www.mhxysy.entity.server.Server;
import cn.edu.sgu.www.mhxysy.pager.server.ServerPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.server.ServerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "服务器管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/server", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class ServerController {

	private final ServerService serverService;

	@Autowired
	public ServerController(ServerService serverService) {
		this.serverService = serverService;
	}

	@ApiOperation("添加服务器")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated ServerDTO serverDTO) {
		serverService.insert(serverDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID删除服务器")
	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(
			@ApiParam(name = "id", value = "服务器ID", required = true)
			@NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
		serverService.deleteById(id);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改服务器信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated ServerDTO serverDTO) {
		serverService.updateById(serverDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("查询全部服务器")
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<Server>> selectAll() {
		List<Server> list = serverService.selectAll();

		return JsonResult.success(null, list);
	}

	@ApiOperation("通过ID查询服务器信息")
	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<Server> selectById(
			@ApiParam(name = "id", value = "服务器ID", required = true)
			@NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
		Server server = serverService.selectById(id);

		return JsonResult.success(null, server);
	}

	@ApiOperation("通过大区ID查询服务器列表")
	@RequestMapping(value = "/selectByAreaId", method = RequestMethod.GET)
	public JsonResult<List<Server>> selectByAreaId(
			@ApiParam(name = "areaId", value = "大区ID", required = false)
			@RequestParam(required = false) Integer areaId) {
		List<Server> list = serverService.selectByAreaId(areaId);

		return JsonResult.success(null, list);
	}

	@ApiOperation("分页查询服务器列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
	public JsonResult<JsonPage<Server>> selectByPage(ServerPager pager) {
		Page<Server> result = serverService.selectByPage(pager);

		return JsonResult.restPage(result);
	}

}